package com.quanyan.assets.web;

import com.quanyan.api.APIRequest;
import com.quanyan.api.APIResponse;
import com.quanyan.api.enums.AssetsPayReceive;
import com.quanyan.api.enums.AssetsType;
import com.quanyan.assets.emun.StatusType;
import com.quanyan.assets.request.ReqAdminAddEggs;
import com.quanyan.assets.request.ReqAdminAddEggsQuery;
import com.quanyan.assets.request.ReqUserAccumulateHistory;
import com.quanyan.assets.response.RespAdminAddEggs;
import com.quanyan.assets.response.RespAdminUserAccumulateHistory;
import com.quanyan.assets.response.RespUserAccumulateHistory;
import com.quanyan.assets.service.EggsService;
import com.quanyan.assets.service.UserAccumulateHistoryService;
import com.quanyan.common.Plugin.PageObj;
import com.quanyan.common.constants.Constants;
import com.quanyan.common.enums.BaseEnum;
import com.quanyan.common.utils.StringUtil;
import com.quanyan.user.response.RespUserInfoBase;
import com.quanyan.user.serviceFacade.UserServiceFacade;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.util.Collections;
import java.util.List;

/**
 * Created by wmb on 2016/8/18.
 */
@RestController
@RequestMapping("/admin/api/assets/eggs")
public class AdminEggsController {
    @Autowired
    private UserAccumulateHistoryService userAccumulateHistoryService;
    @Autowired
    private EggsService eggsService;
    @Autowired
    private UserServiceFacade userServiceFacade;

    //查看用户鸟蛋收支明细
    @RequestMapping(value = "history", method = RequestMethod.POST)
    public APIResponse<RespAdminUserAccumulateHistory> history(@RequestBody ReqUserAccumulateHistory reqUserAccumulateHistory) throws ParseException {
        String mobile = reqUserAccumulateHistory.getMobile();
        if (StringUtil.isNotBlank(mobile)) {
            APIResponse<RespUserInfoBase> apiResponse = userServiceFacade.queryUserInfoByPhone(mobile);
            if (apiResponse != null && apiResponse.isRet() && apiResponse.getData() != null) {
                reqUserAccumulateHistory.setUid(apiResponse.getData().getUid());
                reqUserAccumulateHistory.setMobile(null);
            }
        }
        reqUserAccumulateHistory.setAssetsType(AssetsType.ASSETS_EGGS.id.byteValue());//只查鸟蛋资产类型
        APIRequest<ReqUserAccumulateHistory> apiRequest = new APIRequest<>();
        apiRequest.setData(reqUserAccumulateHistory);
        reqUserAccumulateHistory.setStatus(StatusType.EFFECTIVE.getId().toString());
        reqUserAccumulateHistory.setStatusList(Collections.singletonList(StatusType.EFFECTIVE.getId().byteValue()));
        return userAccumulateHistoryService.getAdminUserAccumulateHistory(apiRequest);
    }

    @RequestMapping(value="rechangeHistory",method = RequestMethod.POST)
    public APIResponse<PageObj<List<RespUserAccumulateHistory>>> rechangeHistory(@RequestBody ReqUserAccumulateHistory reqUserAccumulateHistory) throws ParseException {
        APIRequest apiRequest=new APIRequest();
        if(null==reqUserAccumulateHistory){
            reqUserAccumulateHistory=new ReqUserAccumulateHistory();
        }
        reqUserAccumulateHistory.setAssetsType(AssetsType.ASSETS_EGGS.id.byteValue());
        reqUserAccumulateHistory.setBusinessCode(Constants.ASSETS_EGGS_RECHANGE_CODE);
        reqUserAccumulateHistory.setSystemCode(Constants.ASSETS_SYSTEM_CODE);
        if(null!=reqUserAccumulateHistory.getPayId()){
            if(reqUserAccumulateHistory.getPayId().equals(AssetsPayReceive.ALIRECHARGE.id.byteValue())){
                reqUserAccumulateHistory.setPayReceive(AssetsPayReceive.ALIRECHARGE.getName());
            }else if(reqUserAccumulateHistory.getPayId().equals(AssetsPayReceive.WEIXRECHARGE.id.byteValue())){
                reqUserAccumulateHistory.setPayReceive(AssetsPayReceive.WEIXRECHARGE.getName());
            }
        }
        if(null!=reqUserAccumulateHistory.getStatusList()&&!reqUserAccumulateHistory.getStatusList().isEmpty()){
            String str="";
            for(Byte b:reqUserAccumulateHistory.getStatusList()){
                str=str+b+",";
            }
            str=str.substring(0,str.length()-1);
            reqUserAccumulateHistory.setStatus(str);
        }else{
            reqUserAccumulateHistory.setStatus(null);
        }
        apiRequest.setData(reqUserAccumulateHistory);
        return userAccumulateHistoryService.getUserAccumulateHistory(apiRequest);
    }

    @RequestMapping("/addEggs")
    public APIResponse<BaseEnum> addEggs(@RequestBody ReqAdminAddEggs reqAdminAddEggs){
        return eggsService.addEggs(reqAdminAddEggs);
    }

    @RequestMapping("/queryAddEggs")
    public APIResponse<PageObj<List<RespAdminAddEggs>>> queryAddEggs(@RequestBody ReqAdminAddEggsQuery reqAdminAddEggsQuery) throws ParseException {
        return eggsService.queryAddEggs(reqAdminAddEggsQuery);
    }

    @RequestMapping(value="/health")
    public APIResponse<BaseEnum> health(){
        return APIResponse.returnSuccess();
    }
}
